package com.citcc.web.controller.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by User on 2017/3/3.
 */
public abstract class DateUtil {

    private static final String DEFAULT_PATTEN = "yyyy-MM-dd HH:mm:ss";
    private static final String STATIS_PATTEN = "yyyy-MM-dd";
    private static final String STATIS_PATTEN_YMD = "yyyy/MM/dd";
    private static final String NEW_PATTEN = "yyyyMMddHHmmssSSS";
    private static final String YMDHMS_PATTEN = "yyyyMMddHHmmss";
    private static final String PATTEN_YMD = "yyyyMMdd";


    public static Date now() {
        return new Date();
    }

    public static long current() {
        return System.currentTimeMillis();
    }

    public static Date newDate(int year, int month, int day, int hour, int minute, int second, int millis) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day, hour, minute, second);
        calendar.set(Calendar.MILLISECOND, millis);
        return calendar.getTime();
    }


    public static String format(Date date, String pattern) {
        return new SimpleDateFormat(pattern).format(date);
    }

    public static Date parse(String date, String pattern) {
        try {
            return new SimpleDateFormat(pattern).parse(date);
        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static String format(Date date) {
        return format(date, DEFAULT_PATTEN);
    }

    public static Date parse(String date) {
        return parse(date, DEFAULT_PATTEN);
    }


    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

    /**
     * 获取当前时间对应的文件夹名
     *
     * @return
     */
    public static String getDateFolderName() {
        return getDateFolerName(new Date());
    }

    /**
     * 获取指定日期对应的文件夹名
     *
     * @param date
     * @return
     */
    public static String getDateFolerName(Date date) {
        return simpleDateFormat.format(date);
    }

    /**
     * 获取指定时间戳对应的文件夹名
     *
     * @param time
     * @return
     */
    public static String getDateFolerName(Long time) {
        Date date = new Date();
        date.setTime(time);
        return getDateFolerName(date);
    }

    /**
     * 根据时间戳获取格式化日期
     *
     * @param time
     * @return
     */
    public static String getDateByTime(Long time, String format) {
        Date date = new Date();
        date.setTime(time);
        return format(date, format);
    }

    /**
     * 获取当前时间年月日
     *
     * @param
     * @return
     */
    public static Date getYMD() {
        Date date = new Date();
        String format = format(date, STATIS_PATTEN);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.format(date);
        Date parse = null;
        try {
            parse = sdf.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 获取当前时间年月日
     *
     * @param
     * @return
     */
    public static Date getYMD(Date date) {
        String format = format(date, STATIS_PATTEN_YMD);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        sdf.format(date);
        Date parse = null;
        try {
            parse = sdf.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 获取当前时间年月日
     *
     * @param
     * @return
     */
    public static Date getMonthFirstYMD() {
        //获取前月的第一天
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, 0);
        cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String format = sdf.format(cal_1.getTime());
        Date parse = null;
        try {
            parse = sdf.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 获取当前时间年月日
     *
     * @param
     * @return
     */
    public static Date getMonthEndYMD() {
        //获取前月的第一天
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH, cale.getActualMaximum(Calendar.DAY_OF_MONTH));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String format = sdf.format(cale.getTime());
        Date parse = null;
        try {
            parse = sdf.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 获取当前时间年月日对应文件夹
     *
     * @param
     * @return
     */
    public static String getYFolder() {
        Calendar date = Calendar.getInstance();
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy ");
        SimpleDateFormat format2 = new SimpleDateFormat("MM ");
        SimpleDateFormat format3 = new SimpleDateFormat("dd ");
        String name1 = format1.format(date.getTime());
        return name1;
    }

    /**
     * 获取当前时间年月日对应文件夹
     *
     * @param
     * @return
     */
    public static String getMFolder() {
        Calendar date = Calendar.getInstance();
        SimpleDateFormat format2 = new SimpleDateFormat("MM ");
        String name2 = format2.format(date.getTime());
        return name2;
    }

    /**
     * 获取当前时间年月日对应文件夹
     *
     * @param
     * @return
     */
    public static String getDFolder() {
        Calendar date = Calendar.getInstance();
        SimpleDateFormat format3 = new SimpleDateFormat("dd ");
        String name3 = format3.format(date.getTime());
        return name3;
    }

    /**
     * 获取当前时间年月日对应文件夹
     *
     * @param
     * @return
     */
    public static String getYMDFolder() {
        Calendar date = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String name = format.format(date.getTime());
        return name;
    }

    /**
     * 获取本周一时间
     *
     * @param
     * @return
     */
    public static Date getThisWeekMonday() {
        SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd");
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 1);
        Date date = c.getTime();
        Date ymd = getYMD(date);
        return ymd;
    }

    /* * 获取当月第一天
     * @param
     * @return
     */
    public static Date getTheFirstMonthDay() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        Date date = cal.getTime();
        Date ymd = getYMD(date);
        return ymd;
    }

    /**
     * 根据日期字符串判断当月第几周
     *
     * @param
     * @return
     * @throws Exception
     */
    public static int getWeek(Date time) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周的第一天。
        int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
        return weekOfMonth;
    }

    /**
     * 根据日期字符串判断当月共几周
     *
     * @param
     * @return
     * @throws Exception
     */
    public static int getWeekNum(Date time) {
        Date lastDayOfMonth = getLastDayOfMonth(time);
        int week = getWeek(lastDayOfMonth);
        return week;
    }

    /**
     * 根据日期字符串判断当月第几周
     *
     * @param
     * @return
     * @throws Exception
     */
    public static String getWeekAndYear(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(time);
        Date date = null;
        try {
            date = sdf.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        //第几周
        int year = calendar.get(Calendar.YEAR);
        int week = calendar.get(Calendar.WEEK_OF_YEAR);
        //第几天，从周日开始
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        String weekstring = "";
        if (week < 10) {
            weekstring = "0" + week;
        } else {
            weekstring = Integer.toString(week);
        }
        String YM = Integer.toString(year) + weekstring;
        return YM;
    }

    /**
     * 根据日期字符串判断当月第几周
     *
     * @param
     * @return
     * @throws Exception
     */
    public static int getTheWeekNum(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(time);
        Date date = null;
        try {
            date = sdf.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return day;
    }

    /**
     * 根据日期字符串判断当月第几月
     *
     * @param
     * @return
     * @throws Exception
     */
    public static String getTheMonthNum(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(time);
        Date date = null;
        try {
            date = sdf.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        int year = calendar.get(Calendar.YEAR);
        int week = calendar.get(Calendar.WEEK_OF_YEAR);
        String weekstring = "";
        if (week < 10) {
            weekstring = "0" + week;
        } else {
            weekstring = Integer.toString(week);
        }
        String YM = Integer.toString(year) + weekstring;
        return YM;
    }

    /**
     * 获取当前时间年月日
     *
     * @param
     * @return
     */
    public static Date getWeekFirstYMD() {
        //获取前月的第一天
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String format = sdf.format(cal_1.getTime());
        Date parse = null;
        try {
            parse = sdf.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 获取当前时间年月日
     *
     * @param
     * @return
     */
    public static Date getWeekEndYMD() {
        //获取前月的第一天
        Calendar cale = Calendar.getInstance();
        cale.setFirstDayOfWeek(Calendar.MONDAY);
        cale.setMinimalDaysInFirstWeek(7);
        cale.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String format = sdf.format(cale.getTime());
        Date parse = null;
        try {
            parse = sdf.parse(format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static Date formatDate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static String formatDateTString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String parse = sdf.format(date);
        return parse;
    }

    public static String formatYearAndMonth(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String parse = sdf.format(date);
        return parse;
    }

    public static String formatMonthAndDay(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        String parse = sdf.format(date);
        return parse;
    }


    public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date lastDayOfMonth = calendar.getTime();
        return lastDayOfMonth;
    }

    /**
     * 获取当前时间年月日
     *
     * @param
     * @return
     */
    public static Date getFirstDayOfMonth(Date date) {
        //获取前月的第一天
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.setTime(date);
        cal_1.add(Calendar.MONTH, 0);
        cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        cal_1.set(Calendar.HOUR_OF_DAY, 0);
        cal_1.set(Calendar.MINUTE, 0);
        cal_1.set(Calendar.SECOND, 0);
        return cal_1.getTime();
    }

    public static String formatDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String parse = sdf.format(date);
        return parse;
    }

    public static String fmt(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String parse = sdf.format(date);
        return parse;
    }

    public static String fmtNotify(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String parse = sdf.format(date);
        return parse;
    }

    public static Long parseToDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String showDateFormat(Date date) {
        Calendar nowtime = Calendar.getInstance();
        int nowYear = nowtime.get(Calendar.YEAR);
        int nowMonth = nowtime.get(Calendar.MONTH) + 1;
        int nowdayMonth = nowtime.get(Calendar.DAY_OF_MONTH);
        int nowhourMonth = nowtime.get(Calendar.HOUR_OF_DAY);
        int nowminuteMonth = nowtime.get(Calendar.MINUTE);
        int nowsecondMonth = nowtime.get(Calendar.SECOND);
        Calendar datetime = Calendar.getInstance();
        datetime.setTime(date);
        int datetimeYear = datetime.get(Calendar.YEAR);
        int datetimeMonth = datetime.get(Calendar.MONTH) + 1;
        int datetimedayMonth = datetime.get(Calendar.DAY_OF_MONTH);
        int datetimehourMonth = datetime.get(Calendar.HOUR_OF_DAY);
        int datetimeminuteMonth = datetime.get(Calendar.MINUTE);
        int datetimesecondMonth = datetime.get(Calendar.SECOND);
        //年月日时分相同
       /*  if (nowYear==datetimeYear&&nowMonth==datetimeMonth&&nowdayMonth==datetimedayMonth&&nowhourMonth==datetimehourMonth&&nowminuteMonth==datetimeminuteMonth){
            int i=nowsecondMonth-datetimesecondMonth;
            return i+"秒前";
        }else if ((nowYear==datetimeYear&&nowMonth==datetimeMonth&&nowdayMonth==datetimedayMonth&&nowhourMonth==datetimehourMonth)){
            int i=nowminuteMonth-datetimeminuteMonth;
            return i+"分钟前";
        }else if (nowYear==datetimeYear&&nowMonth==datetimeMonth&&nowdayMonth==datetimedayMonth){
            int i=nowhourMonth-datetimehourMonth;
            return i+"小时前";
        }else if (nowYear==datetimeYear&&nowMonth==datetimeMonth){
            if (nowdayMonth-datetimedayMonth==1){
                //时
                String min = datetimeminuteMonth >= 10 ? ""+datetimeminuteMonth : "0" + datetimeminuteMonth;
                String hour = datetimehourMonth >= 10 ? ""+datetimehourMonth : "0" + datetimehourMonth;
                String sec = datetimesecondMonth >= 10 ? ""+datetimesecondMonth : "0" + datetimesecondMonth;
                //分
                //秒
                return "昨天"+hour+":"+min+":"+sec;
            }else {
                return formatDate(date);
            }
        }*/
        long now = System.currentTimeMillis();
        long other = date.getTime();
        long num = now - other;
        if (num <= 1000 * 60 * 1) {
            return "刚刚";
        } else if (1000 * 60 * 1 < num && num <= 1000 * 60 * 60) {
            int i = Math.round((num / (1000 * 60)));
            return i + "分钟前";
        } else if (1000 * 60 * 60 * 1 < num && num <= 1000 * 60 * 60 * 24) {
            int i = Math.round(num / (1000 * 60 * 60));
            return i + "小时前";
        } else if (nowYear == datetimeYear && nowMonth == datetimeMonth) {
            if (nowdayMonth - datetimedayMonth == 1) {
                //时
                String min = datetimeminuteMonth >= 10 ? "" + datetimeminuteMonth : "0" + datetimeminuteMonth;
                String hour = datetimehourMonth >= 10 ? "" + datetimehourMonth : "0" + datetimehourMonth;
                String sec = datetimesecondMonth >= 10 ? "" + datetimesecondMonth : "0" + datetimesecondMonth;
                //分
                //秒
                return "昨天" + hour + ":" + min + ":" + sec;
            } else {
                return formatDate(date);
            }
        }
        return formatDate(date);
    }

    public static boolean isToday(Date date) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        if (fmt.format(date).toString().equals(fmt.format(new Date()).toString())) {//格式化相同格式
            return true;
        } else {
            return false;
        }


    }

    public static String formatShowDate(Date date) {
        return format(date, "MM-dd");
    }

    public static void main(String[] args) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, -1); //年份减1
        String start =format.format(c.getTime());
        System.out.println(start);
    }

    /**
     * 获得当天零时零分零秒
     *
     * @return
     */
    public static Date initDateByDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date parseaDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parseastringToDate(String date) {
        return parse(date, STATIS_PATTEN);
    }

    public static String notifyDateFormat(Date date) {
        Calendar nowtime = Calendar.getInstance();
        int nowYear = nowtime.get(Calendar.YEAR);
        int nowMonth = nowtime.get(Calendar.MONTH) + 1;
        int nowdayMonth = nowtime.get(Calendar.DAY_OF_MONTH);
        Calendar datetime = Calendar.getInstance();
        datetime.setTime(date);
        int datetimeYear = datetime.get(Calendar.YEAR);
        int datetimeMonth = datetime.get(Calendar.MONTH) + 1;
        int datetimedayMonth = datetime.get(Calendar.DAY_OF_MONTH);


        long now = System.currentTimeMillis();
        long other = date.getTime();
        long num = now - other;
        if (nowYear == datetimeYear && nowMonth == datetimeMonth && nowdayMonth == datetimedayMonth) {
            return fmt(date);
        } else if (nowYear == datetimeYear && nowMonth == datetimeMonth && nowdayMonth - datetimedayMonth == 1) {
            return "昨天";
        }
        return fmtNotify(date);
    }

    public static Date getLastMonthToday() {
        Date date = new Date();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
        date = calendar.getTime();
        return date;
    }
    public static String formatToYMD(Date date) {
        return format(date, STATIS_PATTEN);
    }
    /**
     * 获取当前时间年月日对应文件夹
     *
     * @param
     * @return
     */
    public static String getDay() {
        Calendar date = Calendar.getInstance();
        SimpleDateFormat format3 = new SimpleDateFormat("dd");
        String name1 = format3.format(date.getTime());
        return name1;
    }
    /**
     * 根据日期字符串判断当月第几周
     *
     * @param
     * @return
     * @throws Exception
     */
    public static int getWeek() {

        Calendar calendar = Calendar.getInstance();

        int i = calendar.get(calendar.DAY_OF_WEEK)-1;
        return i;
    }
    public static String formatToSSS(Date date) {
        return format(date, NEW_PATTEN);
    }
    public static String formatToymd(Date date) {
        return format(date, PATTEN_YMD);
    }

    public static String getLastYearDay(){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, -1); //年份减1
        String start =format.format(c.getTime());
        return start;
    }

    /**
     * 获取年月日时分秒
     * @param localDateTime
     * @return
     */
    public static String getYmdhms(LocalDateTime localDateTime){
        DateTimeFormatter df = DateTimeFormatter.ofPattern(YMDHMS_PATTEN);
        String dateTime = df.format(localDateTime);
        return dateTime;
    }
}
